//--------------------------------------
// silk-editor-plugin Project
//
// SilkTextTools.java
// Since: 2010/11/15
//
// $URL$ 
// $Author$
//--------------------------------------
package org.xerial.silk.editor.plugin.text;

import org.eclipse.dltk.ui.text.ScriptSourceViewerConfiguration;
import org.eclipse.dltk.ui.text.ScriptTextTools;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;
import org.eclipse.ui.texteditor.ITextEditor;

public class SilkTextTools extends ScriptTextTools
{

    private final IPartitionTokenScanner scanner;
    private final static String[]        LEGAL_CONTENT_TYPES = SilkPartitions.PARTITION_TYPES;

    public SilkTextTools(boolean autoDisposeOnDisplayDispose) {
        super(SilkPartitions.SILK_PARTITIONING, LEGAL_CONTENT_TYPES, autoDisposeOnDisplayDispose);
        scanner = new SilkPartitionScanner();
    }

    @Override
    public ScriptSourceViewerConfiguration createSourceViewerConfiguraton(IPreferenceStore preferenceStore,
            ITextEditor editor, String partitioning) {
        return new SilkSourceViewerConfiguraion(getColorManager(), preferenceStore, editor, partitioning);
    }

    @Override
    public IPartitionTokenScanner getPartitionScanner() {
        return scanner;
    }

    @Override
    public void setupDocumentPartitioner(IDocument document, String partitioning) {
        IDocumentPartitioner partitioner = createDocumentPartitioner();
        if (partitioner != null) {
            partitioner.connect(document);
            if (document instanceof IDocumentExtension3) {
                IDocumentExtension3 extension3 = (IDocumentExtension3) document;
                extension3.setDocumentPartitioner(partitioning, partitioner);
            }
            else {
                document.setDocumentPartitioner(partitioner);
            }
        }
    }
}
